<template>
  <el-dialog title="分配角色" :visible="dialogVisible">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center" class="dialog-footer">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取 消</el-button>
        <el-button type="primary" size="small" @click="btnOk">确 定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { assignRoles } from '@/api/employees'
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 存储角色的列表
      roleIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 点击取消按钮
    btnCancel() {
      this.roleIds = []
      this.$emit('update:dialogVisible', false)
    },
    // 点击确定按钮
    async btnOk() {
      await assignRoles({ roleIds: this.roleIds, id: this.userId })
      this.$message.success('分配角色成功')
      this.$emit('update:dialogVisible', false)
    },
    // 获取所有角色
    async getRoleList() {
      const data = await getRoleList({ page: 1, size: 100 })
      this.list = data.rows
      console.log(data)
    },
    // 获取已有的角色
    async getUserDetailById(id) {
      const data = await getUserDetailById(id)
      console.log(data)
      this.roleIds = data.roleIds // 赋值本用户的角色
    }
  }
}
</script>

<style></style>
